package com.ruoyi.custom.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.custom.dto.SelectTreeDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.custom.mapper.StoreCategoryMapper;
import com.ruoyi.custom.domain.StoreCategory;
import com.ruoyi.custom.service.IStoreCategoryService;

import javax.annotation.Resource;

/**
 * 商品分类Service业务层处理
 * 
 * @author xking
 * @date 2022-08-02
 */
@Service
public class StoreCategoryServiceImpl implements IStoreCategoryService 
{
    @Resource
    private StoreCategoryMapper storeCategoryMapper;

    /**
     * 查询商品分类
     * 
     * @param id 商品分类主键
     * @return 商品分类
     */
    @Override
    public StoreCategory selectStoreCategoryById(Long id)
    {
        return storeCategoryMapper.selectStoreCategoryById(id);
    }

    /**
     * 查询商品分类列表
     * 
     * @param storeCategory 商品分类
     * @return 商品分类
     */
    @Override
    public List<StoreCategory> selectStoreCategoryList(StoreCategory storeCategory)
    {
        return storeCategoryMapper.selectStoreCategoryList(storeCategory);
    }

    /**
     * 新增商品分类
     * 
     * @param storeCategory 商品分类
     * @return 结果
     */
    @Override
    public int insertStoreCategory(StoreCategory storeCategory)
    {
        return storeCategoryMapper.insertStoreCategory(storeCategory);
    }

    /**
     * 修改商品分类
     * 
     * @param storeCategory 商品分类
     * @return 结果
     */
    @Override
    public int updateStoreCategory(StoreCategory storeCategory)
    {
        return storeCategoryMapper.updateStoreCategory(storeCategory);
    }

    /**
     * 批量删除商品分类
     * 
     * @param ids 需要删除的商品分类主键
     * @return 结果
     */
    @Override
    public int deleteStoreCategoryByIds(Long[] ids)
    {
        return storeCategoryMapper.deleteStoreCategoryByIds(ids);
    }

    /**
     * 删除商品分类信息
     * 
     * @param id 商品分类主键
     * @return 结果
     */
    @Override
    public int deleteStoreCategoryById(Long id)
    {
        return storeCategoryMapper.deleteStoreCategoryById(id);
    }

    @Override
    public List<SelectTreeDTO> selectStoreCategoryListTree() {

        List<StoreCategory> storeCategories = storeCategoryMapper.selectStoreCategoryList(new StoreCategory(0L));

        List<SelectTreeDTO> selectTreeDTOList = new ArrayList<>();
        for (StoreCategory storeCategory : storeCategories) {
            SelectTreeDTO selectTreeDTO = new SelectTreeDTO();
            selectTreeDTO.setValue(storeCategory.getId());
            selectTreeDTO.setLabel(storeCategory.getCateName());
            selectTreeDTO.setChildren(recursiveData(new ArrayList<>(), storeCategory.getId()));
            selectTreeDTOList.add(selectTreeDTO);
        }

        return selectTreeDTOList;
    }

    @Override
    public List<SelectTreeDTO> selectStoreCategoryListSelect() {
        List<StoreCategory> storeCategories = storeCategoryMapper.selectStoreCategoryList(new StoreCategory(0L));
        List<SelectTreeDTO> selectTreeDTOList = new ArrayList<>();
        for (StoreCategory storeCategory : storeCategories) {
            SelectTreeDTO selectTreeDTO = new SelectTreeDTO();
            selectTreeDTO.setValue(storeCategory.getId());
            selectTreeDTO.setLabel("|-----" + storeCategory.getCateName());
            selectTreeDTO.setDisabled(true);
            selectTreeDTOList.add(selectTreeDTO);
            recursiveDataSelect(selectTreeDTOList, storeCategory.getId());
        }
        return selectTreeDTOList;
    }

    private List<SelectTreeDTO> recursiveDataSelect(List<SelectTreeDTO> selectTreeDTOList, Long parentId) {
        List<StoreCategory> storeCategories = storeCategoryMapper.selectStoreCategoryList(new StoreCategory(parentId));
        if (storeCategories.isEmpty()){
            return selectTreeDTOList;
        }
        storeCategories.forEach(item ->{
            SelectTreeDTO selectTreeDTO = new SelectTreeDTO();
            selectTreeDTO.setValue(item.getId());
            selectTreeDTO.setLabel("|-----|-----" + item.getCateName());
            selectTreeDTOList.add(selectTreeDTO);
            recursiveDataSelect(selectTreeDTOList, item.getId());
        });

        return selectTreeDTOList;
    }
    private List<SelectTreeDTO> recursiveData(ArrayList<SelectTreeDTO> selectTreeDTOS, Long parentId) {
        List<StoreCategory> storeCategories = storeCategoryMapper.selectStoreCategoryList(new StoreCategory(parentId));
        if (storeCategories.isEmpty()){
            return selectTreeDTOS;
        }
        storeCategories.forEach(item ->{
            SelectTreeDTO selectTreeDTO = new SelectTreeDTO();
            selectTreeDTO.setValue(item.getId());
            selectTreeDTO.setLabel(item.getCateName());
            List<SelectTreeDTO> list = recursiveData(new ArrayList<SelectTreeDTO>(), item.getId());
            selectTreeDTO.setChildren(list.size() == 0 ? new ArrayList<>() : list);
            selectTreeDTOS.add(selectTreeDTO);
        });

        return selectTreeDTOS;
    }

}
